using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using eYap.sources.Net;
using agsXMPP.protocol.iq.vcard;
using System.IO;
using agsXMPP.protocol.component;
using agsXMPP.protocol.extensions;
using eYap.src.Forms;
using eYap.Properties;
using System.Configuration;
using System.Xml;
using eYap.src;

namespace eYap
{
    public partial class MainForm : Form
    {
        public FBManager fbclient;
        public ImageList imageList;

        public MainForm()
        {
            InitializeComponent();

            pBox_FBLogo.Image = eYap.Properties.Resources.facebook_logo;

            cBox_FacebookAccounts.Items.Clear();
            SettingsManager.ReadAccounts();
            PopulateProtocolComboBox();

            imageList = new ImageList();
            imageList.ImageSize = new Size(32, 32);
            lstView_ContactList.LargeImageList = imageList;

            //Setup the log file
            using (FileStream fs = new FileStream("log.txt", FileMode.Create, FileAccess.Write))
            {
            }

            fbclient = new FBManager(this);
            fbclient.OnLoginSuccess += new FBManager.LoginSuccessHandler(fbclient_OnLoginSuccess);
            fbclient.OnGetMyVCard += new FBManager.GetMyVCardHandler(fbclient_OnGetMyVCard);
            fbclient.OnLogout += new FBManager.LogoutHandler(fbclient_OnLogout);

        }

        void fbclient_OnGetMyVCard(agsXMPP.protocol.client.IQ iqVcard)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new FBManager.GetMyVCardHandler((fbclient_OnGetMyVCard)), new object[] { iqVcard });
            }
            else
            {
                if (iqVcard.Type == agsXMPP.protocol.client.IqType.result)
                {
                    if (iqVcard.Vcard != null)
                    {
                        Photo photo = iqVcard.Vcard.Photo;
                        if (photo != null)
                        {
                            pbox_MyProfileImage.Image = iqVcard.Vcard.Photo.Image;
                            pbox_MyProfileImage.SizeMode = PictureBoxSizeMode.StretchImage;
                            lbl_MyName.Text = iqVcard.Vcard.Fullname;
                            fbclient.myInfo = new MyInfo(iqVcard.Vcard.Fullname, iqVcard.Vcard.Photo.Image);
                        }
                        else
                        {
                            pbox_MyProfileImage.Image = iqVcard.Vcard.Photo.Image;
                            pbox_MyProfileImage.SizeMode = PictureBoxSizeMode.StretchImage;
                            lbl_MyName.Text = iqVcard.Vcard.Fullname;
                            fbclient.myInfo = new MyInfo(iqVcard.Vcard.Fullname, Image.FromFile("profile.jpg"));
                        }
                    }

                }
            }
        }

        void fbclient_OnLogout()
        {
            sBarLbl_Updates.Text = "Disconnected from Facebook Chat";
        }

        void fbclient_OnLoginSuccess()
        {
            sBarLbl_Updates.Text = "Connected To Facebook Chat";
        }

        private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fbclient.Disconnect();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void qconDropDownItems_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            fbclient.Connect(item.Text, Cryptor.Decrypt((string)item.Tag, true));
        }

        private void lstView_ContactList_DoubleClick(object sender, EventArgs e)
        {
            ListView lv = (ListView)sender;
            foreach (ContactItem item in lv.Items)
            {
                if (item.Selected)
                {
                    CreateChatTab(item);
                }
            }
        }

        public void CreateChatTab(ContactItem item)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MethodInvoker(delegate() { CreateChatTab(item); }));
            }
            else
            {
                TabPage tab = new TabPage();
                tab.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
                ChatControl chatCtrl = new ChatControl();
                chatCtrl.GetContactReference(item.contact);
                tab.Controls.Add(chatCtrl);
                tab.Text = item.contact.Name;
                tbCtrl_Main.TabPages.Add(tab);
                item.contact.GetUIReferences(tab, chatCtrl);
            }
        }

        private void accountsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AccountManager amgr = new AccountManager(this);
            amgr.ShowDialog(this);
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog();
            
        }

        public string FetchPassword(string username)
        {
            foreach (Account acc in SettingsManager.ACCOUNTS)
            {
                if (acc.username == username)
                    return acc.password;
            }
            return null;
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
           fbclient.Connect(cBox_FacebookAccounts.SelectedItem.ToString(), FetchPassword(cBox_FacebookAccounts.SelectedItem.ToString()));
        }

        public void PopulateProtocolComboBox()
        {
            cBox_FacebookAccounts.Items.Clear();

            foreach(eYap.src.Account account in SettingsManager.ACCOUNTS)
            {
                if(account.protocol == SettingsManager.Protocol.Facebook)
                {
                    cBox_FacebookAccounts.Items.Add(account.username);
                }
            }
            if (cBox_FacebookAccounts.Items.Count > 0)
            {
                cBox_FacebookAccounts.SelectedIndex = 0;
            }
        }


    }
}